package me.pplay.playtalk.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Method;
import java.util.Locale;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;



public class UserInfo {
	private static Context context;
	private static String imsi="999000123456888";
	private static String imei="999000123456888";
	private static int appid=0;
	private static int appver=0;
	private static int srcw=0;
	private static int srch=0;
	private static int mem=0;
	private static String lang="EN";
	private static int currentapiVersion=0; 
	private static String hsman="skytest";
	private static String hstype="m900";
	private static TelephonyManager tm;
	private static String Uid="";
	// //����MCC+MNC���� (SIM����Ӫ�̹�Ҵ������Ӫ���������)(IMSI)
	// String mccMnc = tm.getNetworkOperator();
	// String imei = tm.getDeviceId(); //ȡ��IMEI
	// String tel = tm.getLine1Number(); //ȡ��MSISDN���ܿ���Ϊ��
	// String ICCID =tm.getSimSerialNumber(); //ȡ��ICCID
	// String imsi =tm.getSubscriberId(); //ȡ��IMSI
	// String huiche ="\n";

	 public static Context getContext() {
		return context;
	}

	 public static void setContext(Context context1 ) {
		context = context1;
	}

	public static String getWifimac() {
		String macAddress = "", ip = null;
		WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
		if (null != info) {
			macAddress = info.getMacAddress();
		//	ip = int2ip(info.getIpAddress());
		}
		System.out.println("mac:" + macAddress );
		return macAddress;
	}
	/**
	 * 获取CPU序列号
	 * @return CPU序列号(16位)
	 * 读取失败为"0000000000000000"
	 */

	public static String getCPUSerial() {
	    String str = "", strCPU = "", cpuAddress = "0000000000000000";
	    try {
	        // 读取CPU信息
	        Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
	        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
	        LineNumberReader input = new LineNumberReader(ir);
	        // 查找CPU序列号
	        for (int i = 1; i < 100; i++) {
	            str = input.readLine();
	            if (str != null) {
	                // 查找到序列号所在行
	                if (str.indexOf("Serial") > -1) {
	                    // 提取序列号
	                    strCPU = str.substring(str.indexOf(":") + 1,
	                    str.length());
	                    // 去空格
	                    cpuAddress = strCPU.trim();
	                    break;
	                }
	            }
	            else {
	                // 文件结尾
	                break;
	            }
	        }
	    }
	    catch (IOException ex) {
	        // 赋予默认值
	        ex.printStackTrace();
	    }
	    return cpuAddress;
	}
	//获取android手机序列号
	public static String getSerialNumber(){  
        String serial = "";       
          try {           
              Class<?> c = Class.forName("android.os.SystemProperties");      
              Method get = c.getMethod("get", String.class);     
              serial = (String) get.invoke(c, "ro.serialno");    
              System.out.println(serial);  
              }   
          catch (Exception ignored) {    
                
          }   
          return serial;  
   }  
	public static String getImsi() {
		tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getSubscriberId() != null){ 
			imsi = tm.getSubscriberId(); // ȡ��IMSI
			//test start 
			imsi = imsi.substring(0, imsi.length()-3)+"888";
		//	imsi="4600123457888";
		}else{
			imsi="999000123456888";
		}
	//	imsi = "";
		//imsi="4600123457888";
		return imsi;
	}

	public static void setImsi(String imsi1) {
		imsi = imsi1;
	}
 
	public static String getImei() {
		tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getDeviceId() != null)
			imei = tm.getDeviceId(); // ȡ��IMEI
//		imei = tm.getDeviceId(); 
		return imei;
	}

	public static void setImei(String imei1) {
		imei = imei1;
	}
	public static int getAppid() {
		return appid;
	}
	public static int getAppver() {
		return appver;
	}

	public static int getSrcw() {
		return srcw;
	}

	public static void setSrcw(int srcw) {
		UserInfo.srcw = srcw;
	}

	public static int getSrch() {
		return srch;
	}

	public static void setSrch(int srch) {
		UserInfo.srch = srch;
	}

	public static String getHsman() {
		return hsman;
	}

	public static void setHsman(String hsman) {
		UserInfo.hsman = hsman;
	}

	public static String getHstype() {
		return hstype;
	}

	public static void setHstype(String hstype) {
		UserInfo.hstype = hstype;
	}
	public static int getMem() {
		return mem;
	}

	public static void setMem(int mem) {
		UserInfo.mem = mem;
	}

	public static String getLang() {
		lang = Locale.getDefault().getLanguage();
		 return lang;
	}

	public static void setLang(String lang) {
		UserInfo.lang = lang;
	}

	public static int getCurrentapiVersion() {
		currentapiVersion=android.os.Build.VERSION.SDK_INT;
		return currentapiVersion;
	}

	public static void setCurrentapiVersion(int currentapiVersion) {
		UserInfo.currentapiVersion = currentapiVersion;
	}
	 public static String bytesAsHexString(byte[] bytes, int maxShowBytes) {
	        int idx = 0;
	        StringBuilder  body = new StringBuilder();
	        body.append("bytes size is:[");
	        body.append(bytes.length);
	        body.append("]\r\n");
	        
	        for ( byte b : bytes) {
	            int hex = (b) & 0xff;
	            String shex = Integer.toHexString(hex).toUpperCase();
	            if ( 1 == shex.length() ) {
	                body.append( "0" );
	            }
	            body.append( shex );
	            body.append( " " );
	            idx++;
	            if ( 16 == idx ) {
	                body.append("\r\n");
	                idx = 0;
	            }
	            maxShowBytes--;
	            if ( maxShowBytes <= 0 ) {
	                break;
	            }
	        }
	        if ( idx != 0 ) {
	            body.append("\r\n");
	        }
	        return  body.toString();
	    }


}
